package com.sakura.hk.sakura.springboot.plugin.task;

import com.sakura.hk.sakura.springboot.plugin.SakuraExtension;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.internal.AbstractTask;
import org.gradle.api.tasks.TaskAction;

import java.io.File;

/**
 * 插件工作前准备阶段
 *
 * @Author: 贺坤
 * @Date: 2021/7/13 13:53
 */
public class SakuraInit extends DefaultTask {
    /**
     * 当前任务名称
     */
    public static final String MY_TASK_NAME = "sakuraInit";
    /**
     * 插件配置项
     */
    private SakuraExtension sakuraExtension;
    /**
     * 长度
     */
    static int size = 50;

    @TaskAction
    public void action() {
        init();
        System.out.println("感谢使用Sakrua插件");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 3; ++i) {
            stringBuilder.append("+");
            for (int j = 0; j < size; ++j) {
                stringBuilder.append("-");
            }
        }
        stringBuilder.append("+\r\n");
        int i = 0 ;
        printTable(stringBuilder,"序号", "名称", "值");
        printTable(stringBuilder,""+(i++), "是否开启自定义模式", String.valueOf(sakuraExtension.getCustom()));
        printTable(stringBuilder,""+(i++), "SpringBoot启动器jar路径*(重要)", " "+sakuraExtension.getSpringBootLoaderJarPath()+" ");
        printTable(stringBuilder,""+(i++), "编译路径", sakuraExtension.getCompileDir().getAbsolutePath());
        printTable(stringBuilder,""+(i++), "缓存路径", sakuraExtension.getCacheDir().getAbsolutePath());
        printTable(stringBuilder,""+(i++), "缓存目录", sakuraExtension.getCacheName());
        printTable(stringBuilder,""+(i++), "构建目录", sakuraExtension.getBuildDir().getAbsolutePath());
        printTable(stringBuilder,""+(i++), "构建jar名称", sakuraExtension.getBuildJarName());
        printTable(stringBuilder,""+(i++), "启动器类名", sakuraExtension.getMyLauncherName());
        printTable(stringBuilder,""+(i++), "启动器包名", sakuraExtension.getMyLauncherPackageName());
        printTable(stringBuilder,""+(i++), "插件JVM路径属性", sakuraExtension.getPluginPathProperty());
        printTable(stringBuilder,""+(i++), "插件文件名称属性", sakuraExtension.getPluginPathNameProperty());
        printTable(stringBuilder,""+(i++), "插件所在文件夹名称", sakuraExtension.getPluginPathName());
        printTable(stringBuilder,""+(i++), "生成jar包的后缀", sakuraExtension.getSuffix());
        printTable(stringBuilder,""+(i++), "是否删除缓存文件", String.valueOf(sakuraExtension.getDelCache()));
        printTable(stringBuilder,""+(i++), "打包类型", sakuraExtension.getType().getValue());
        System.out.println(stringBuilder);
    }

    public static AbstractTask foundMyTask(Project project) {
        SakuraInit task = project.getTasks().create(MY_TASK_NAME, SakuraInit.class);
        task.setDescription("初始化SakuraPlugin");
        task.dependsOn("bootJar");
        return task;
    }

    private void init() {
        this.sakuraExtension = getProject().getExtensions().getByType(SakuraExtension.class);
    }

    /**
     * 打印表格
     * @param stringBuilder
     * @param column0
     * @param column1
     * @param column2
     * @return
     */
    static StringBuilder printTable(StringBuilder stringBuilder ,String column0, String column1, String column2) {
        String[] table = {column0, column1, column2};
        for (int i = 0; i < table.length; ++i) {
            stringBuilder.append("|");
            int len = table[i].length();
            int left_space = (size - len) % 2 == 0 ? (size - len) / 2 : (size - len) / 2 + 1;
            int right_space = (size - len) / 2;
            //左边空格
            for (int j = 0; j < left_space; ++j) {
                stringBuilder.append(" ");
            }
            stringBuilder.append(table[i]);
            //右边空格
            for (int j = 0; j < right_space; ++j) {
                stringBuilder.append(" ");
            }
        }
        stringBuilder.append("|\r\n");
        for (int i = 0; i < table.length; ++i) {
            stringBuilder.append("+");
            for (int j = 0; j < size; ++j) {
                stringBuilder.append("-");
            }
        }
        stringBuilder.append("+\r\n");
        return stringBuilder;
    }

}
